package com.clusterswisdom.horn.api.tmall.action.scitem;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.taobao.api.domain.InventorySum;
import com.taobao.api.response.InventoryAdjustExternalResponse;
import com.taobao.api.response.InventoryInitialResponse;
import com.taobao.api.response.InventoryQueryResponse;
import com.taobao.api.response.ScitemOutercodeGetResponse;

/**
 * 单个商品仓库区域库存调整
 * @author Jin_tian
 *
 */
public class OneStoreExtendAction extends AbstractTmallAction implements HornAction{
	
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param) throws Exception {
//		InventoryAdjustExternalResponse response = (InventoryAdjustExternalResponse)TmallInstance.getInstance().executeRequest(getRequest(param), "", "");
//		
//		if(response.isSuccess()){
//			return new HornResponse(true, "成功");
//			
//		}else{
//			return getUnsuccessResponse(response);
//			
//		}
		return new HornResponse(true, "成功");
	}
	
	/**
	 * 获取调用非交易库存调整的传入参数
	 * @param param
	 * @return
	 * @throws Exception 
	 */
	public JSONObject getRequest(JSONObject param) throws Exception{
		param.put("biz_unique_code", Long.toString(System.currentTimeMillis()));
		param.put("operate_type", "BALANCE");
		param.put("biz_type", "BALANCE");
		param.put("operate_time", new Date());
		param.put("items", getItems(param));
		return null;
	}
	
	/**
	 * 获取传入的items
	 * @param param
	 * @return
	 * @throws Exception 
	 */
	public String getItems(JSONObject param) throws Exception{
		JSONObject items = new JSONObject();
		items.put("scItemId", "0");
		items.put("scItemCode", param.getString("itemId"));
		items.put("inventoryType", "1");
		
		long quanty =getQuanty(param);
		items.put("direction", quanty < 0 ? "-1" : "1");
		items.put("quantity", Math.abs(quanty));
		
		return JSON.toJSONString(items);
	}
	
	public long getQuanty(JSONObject param) throws Exception{
		long newNum = param.getLong("num");
		
		JSONObject request = new JSONObject();
		param.put("outer_code", param.getString("itemId"));
		
		ScitemOutercodeGetResponse response = (ScitemOutercodeGetResponse)TmallInstance.getInstance().executeRequest(param, "scitem", "scitemOutercodeGet");

		long itemId = response.getScItem().getItemId();
		request.put("sc_item_ids", Long.toString(itemId));
		request.put("store_codes", param.getString("store_code"));
		
		InventoryQueryResponse iResponse = (InventoryQueryResponse)TmallInstance.getInstance().executeRequest(param, "scitem", "inventoryquery");
		//如果库存信息不存在则初始化库存,提示库存不存在的错误代码为30014
		if(iResponse.getItemInventorys() == null && iResponse.getErrorCode().equals("30014")){
			request.put("store_code", param.getString("store_code"));
			
			JSONObject items = new JSONObject();
			items.put("scItemId", "0");
			items.put("scItemCode", param.getString("itemId"));
			items.put("inventoryType", "1");
			items.put("quantity",0);
			
			request.put("items", JSON.toJSONString(items));
			InventoryInitialResponse initialResponse = (InventoryInitialResponse)TmallInstance.getInstance().executeRequest(request, "scitem", "inventoryinitial");
			
			return newNum;
		}else{
			long nowNum = 0;
			for(InventorySum inventorySum : iResponse.getItemInventorys()){
				nowNum = inventorySum.getQuantity();
				
			}
			
			return newNum-nowNum;
		}
	}
}
